﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telecom.Service;
using TelecomGis.Helper.Control;

namespace TelecomGis.Controllers
{
    public class Donvi_ToQLController : ContronllerBase
    {
        private readonly donviToSer _donviToSer = new donviToSer();
        private readonly quanhuyenPhuongXaSer _quanhuyenPhuongXaSer = new quanhuyenPhuongXaSer();
        //
        // GET: /Donvi_ToQL/

        public string ListQuanHuyen_All()
        {
            var _listEntity = _quanhuyenPhuongXaSer.getList_QH_All();

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string ListPhuongXa_All(string quanhuyenId)
        {
            var _listEntity = _quanhuyenPhuongXaSer.getList_PX_By_Quan_All(quanhuyenId);

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string Get_DonVi_ById(string maDV)
        {
            var _listEntity = _donviToSer.get_DV_ById(maDV);

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string Get_toql_ById(string maTo)
        {
            var _listEntity = _donviToSer.get_To_ById(maTo);

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string Get_quanhuyen_ById(string maQuan)
        {
            var _listEntity = _quanhuyenPhuongXaSer.GetQuanHuyenById(maQuan);

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string Get_DonVi_To_OnJson()
        {

            var _listEntity =new List<_listJsontoTree>();
            _listEntity.Add(new _listJsontoTree() { id = "", text = "Tất cả đơn vị", Idparent= "", type = "all" });

            _listEntity.AddRange(_donviToSer.getListTree(UtContextgis._UserPageContext.DonViByUser, UtContextgis._UserPageContext.ToKtByuser));

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string GetDonViList_Only()
        {

            var _listEntity = new List<_listJsontoTree>();
            _listEntity.Add(new _listJsontoTree() { id = "", text = "Tất cả đơn vị", type = "all" });

            _listEntity.AddRange(_donviToSer.getListDV_Json());

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string Get_QuanHuyen_PX_To_OnJson()
        {

            var _listEntity = new List<_listJsontoTree_Maps>();
            _listEntity.Add(new _listJsontoTree_Maps() { id = "", text = "Chọn quận huyện", type = "all", clong = "", clat ="" });

            _listEntity.AddRange(_quanhuyenPhuongXaSer.getListTree(UtContextgis._UserPageContext.QuanHuyenByUser, UtContextgis._UserPageContext.PhuongxaByUser));

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string Get_DonVi_To_OnJson_InsertText()
        {

            var _listEntity = new List<_listJsontoTree>();
            _listEntity.Add(new _listJsontoTree() { id = "", text = "--Chọn đơn vị quản lý--", type = "all" });
            _listEntity.AddRange(_donviToSer.getListTree(UtContextgis._UserPageContext.DonViByUser, UtContextgis._UserPageContext.ToKtByuser));

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string Get_DonVi_To_OnJson_NotFindType()
        {

            var _listEntity = new List<_listJsontoTree>();
            _listEntity.Add(new _listJsontoTree() { id = "", Idparent = "", text = "--Tất cả đơn vị--", type = "all" });
            _listEntity.AddRange(_donviToSer.getListTree(UtContextgis._UserPageContext.DonViByUser, UtContextgis._UserPageContext.ToKtByuser));

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string Get_DonVi_To_OnJson_NotFindType_AndSetParent()
        {

            var _listEntity = new List<_listJsontoTree>();
            _listEntity.Add(new _listJsontoTree() { id = "", Idparent = "", text = "--Tất cả đơn vị--", type = "all" });
            _listEntity.AddRange(_donviToSer.getListTreeFindSure(UtContextgis._UserPageContext.DonViByUser, UtContextgis._UserPageContext.ToKtByuser));

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string Get_DonVi_To_OnJson_NotFindType_AndSetParent_All()
        {

            var _listEntity = new List<_listJsontoTree>();
            //_listEntity.Add(new _listJsontoTree() { id = "", Idparent = "", text = "--Tất cả đơn vị--", type = "all" });
            _listEntity.AddRange(_donviToSer.getListTreeFindSure_all());

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }



        public string Get_ToAL_To_OnJson_InsertText(string ma)
        {

            var _listEntity = _donviToSer.getListTO(ma, UtContextgis._UserPageContext.ToKtByuser);

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public string getListDV()
        {
            var _listEntity = _donviToSer.getListDV();

            return JsonConvert.SerializeObject(_listEntity, Formatting.Indented,
                              new JsonSerializerSettings
                              {
                                  ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                              });
        }

        public ActionResult Index()
        {
            return View();
        }

    }
}
